package com.ita.mutiple;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class FileCopyThread extends Thread {

	private String srcPath;
	private String desPath;
	private int begin;
	private int end;

	public FileCopyThread(String srcPath, String desPath, int begin, int end) {
		super();
		this.srcPath = srcPath;
		this.desPath = desPath;
		this.begin = begin;
		this.end = end;
	}

	public String getSrcPath() {
		return srcPath;
	}

	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}

	public String getDesPath() {
		return desPath;
	}

	public void setDesPath(String desPath) {
		this.desPath = desPath;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public void run() {
		try {
			RandomAccessFile in = new RandomAccessFile(srcPath, "r");
			RandomAccessFile out = new RandomAccessFile(desPath, "rw");
			FileChannel inChannel = in.getChannel();
			FileChannel oChannel = out.getChannel();
			in.seek(begin);
			out.seek(begin);
			FileLock lock = oChannel.lock(begin, end-begin, false);
			inChannel.transferTo(begin, end-begin, oChannel);
			lock.release();
			out.close();
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

}
